package com.baimawangzi.recovery.service.impl;

import com.baimawangzi.recovery.bean.PageBean;
import com.baimawangzi.recovery.bean.User;
import com.baimawangzi.recovery.dao.UserDao;
import com.baimawangzi.recovery.dao.impl.UserDaoImpl;
import com.baimawangzi.recovery.service.UserService;

import java.util.List;

public class UserServiveImpl implements UserService {
    private UserDao userDao=new UserDaoImpl();
    @Override
    public List<User> findByLimit(int m, int n) {
        return userDao.findByLimit(m,n);
    }

    @Override
    public PageBean<User> getPageBean(int currentPage, int pageSize) {

        PageBean<User> pageBean = new PageBean<>();
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalSize(userDao.getCount());
        if (pageBean.getTotalSize()%pageSize==0){
            pageBean.setTotalPage(pageBean.getTotalSize()/pageSize);
        }
        else {
            pageBean.setTotalPage(pageBean.getTotalSize()/pageSize+1);
        }
        int m =(currentPage-1)*pageSize;
        pageBean.setData(userDao.findByLimit(m,pageSize));
        return pageBean;
    }

    @Override
    public int add(User user) {
        return userDao.add(user);
    }

    @Override
    public int delete(int id) {
        return userDao.delete(id);
    }

    @Override
    public User findById(int id) {
        return userDao.findById(id);
    }

    @Override
    public int update(User user) {
        User olduser = findById(user.getId());
        if(!olduser.getUsername().equals(user.getUsername())){
            //不让改用户名
            return 0;
        }
        if(olduser.getPassword().equals(user.getPassword())){
            //没改密码
            return userDao.updateNoPwd(user);
        }
        return userDao.update(user);
    }

    @Override
    public List<User> getAll() {
        return userDao.getAll();
    }
}
